import express, {Request, Response, Router} from "express";
import {Application, ApplicationRequestHandler, RequestHandler} from "express-serve-static-core";

import http from "http";
import {LoggerFactory} from "../logs/Logger";


const log = LoggerFactory.getLogger('HttpServer');

export class HttpServer {
    private readonly port: number;
    private readonly host: string;
    server: Application = express();
    httpServer: http.Server | null = null;


    constructor(port: number = 6535, host: string = '127.0.0.1') {
        this.host = host;
        this.port = port;
    }

    public addRouter(path:Router): HttpServer {
        this.server.use(path)
        return this;
    }

    public addRequestHandler(plugin: RequestHandler): HttpServer {
        this.server.use(plugin)
        return this;
    }

    public start(callback: () => void = () => {}): HttpServer {
        this.httpServer = this.server.listen(this.port, this.host, callback);
        return this
    }

    public shutdown() {
        this.httpServer?.close(process.exit(0))
    }
}